package com.deke.home.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deke.entity.EduCourse;
import com.deke.entity.TbZone;
import com.deke.home.dao.TbCourseDao;
import com.deke.home.dao.TbZoneDao;
import com.deke.home.service.QianZoneService;
import com.deke.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：Username 刘亦辰（59372312@qq.com）
 * @date ：Created in 2022/12/21 11:23
 * @description：
 * @modified By：
 * @version:
 **/
@Service
public class QianZoneServiceImpl implements QianZoneService {
    @Autowired
    private TbZoneDao zoneDao;

    @Autowired
    private TbCourseDao courseDao;

    /**
     * @create by: Username 刘亦辰 
     * @description:  获取前端可用的的zone
     * @create time: 2022/12/21 11:24
     * @param null
     * @return :  
     **/
    @Transactional
    @Override
    public Result getAllQianZone() {
        QueryWrapper<TbZone> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("is_disable",0);
        List<TbZone> zoneList = zoneDao.selectList(wrapper);
        List<String> CourseIds;
        List<EduCourse> courseList;
        for (TbZone zone : zoneList) {
            CourseIds = zoneDao.queryCourseIdByZoneId(zone.getId());
            if (CourseIds.size()==0){
                break;
            }
            courseList = courseDao.selectBatchIds(CourseIds);
            zone.setCourseList(courseList);
        }
        if (ObjectUtil.isNotEmpty(zoneList) && zoneList.size()>0){
            return new Result(2000,"成功",zoneList);
        }
        return new Result(0000,"失败");
    }
}